<template>
  <el-dialog
    :title="!dataForm.menuId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <!--    <el-form-item label="父级ID" prop="parentId">-->
      <!--      <el-input v-model="dataForm.parentId" placeholder="父级ID"></el-input>-->
      <!--    </el-form-item>-->
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="职责" prop="duty">
        <el-input v-model="dataForm.duty" placeholder="职责"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择部门"
                  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input v-model="dataForm.notes" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        menuId: 0,
        parentId: 0,
        name: '',
        parentName: '',
        duty: '',
        orderNum: '',
        notes: ''
      },
      dataRule: {
        // parentId: [
        //   {required: true, message: '父级ID不能为空', trigger: 'blur'}
        // ],
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        duty: [
          {required: true, message: '职责不能为空', trigger: 'blur'}
        ],
        orderNum: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ],
        notes: [
          {required: true, message: '备注不能为空', trigger: 'blur'}
        ]
      },
      menuList:[],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
    }
  },
  methods: {
    init(id) {
      this.dataForm.menuId = id || 0
      this.visible = true
      this.$http({
        url: this.$http.adornUrl('/resume/department/list'),
        method: 'get',
        params: this.$http.adornParams({"key":""})
      }).then(({data}) => {
        console.log('datadata',data);
        this.menuList = treeDataTranslate(data.page, 'menuId')
        this.menuList.unshift({
          menuId: 0,
          parentId: 0,
          name: '根目录',
          duty: '',
          orderNum: '',
          notes: ''
        })
        console.log(this.menuList);
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.menuId) {
          this.$http({
            url: this.$http.adornUrl(`/resume/department/info/${this.dataForm.menuId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.parentId = data.department.parentId
              this.dataForm.name = data.department.name
              this.dataForm.duty = data.department.duty
              this.dataForm.orderNum = data.department.orderNum
              this.dataForm.notes = data.department.notes
              this.menuListTreeSetCurrentNode()
            }
          })
        }
      })
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle (data, node) {
      this.dataForm.parentId = data.menuId
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode () {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/resume/department/${!this.dataForm.menuId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'menuId': this.dataForm.menuId || undefined,
              'parentId': this.dataForm.parentId,
              'name': this.dataForm.name,
              'duty': this.dataForm.duty,
              'orderNum': this.dataForm.orderNum,
              'notes': this.dataForm.notes
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
